package com.union.core.sys.dao.impl;

import java.util.*;
import java.sql.ResultSet;
import org.apache.log4j.Logger;

import com.union.core.sys.model.SysLogs;
import com.hnjowe.webdb.*;
import com.union.core.sys.dao.SysLogsDao;

/**
 * DAO实现类: 日志管理
 * filename: SysLogsDaoImpl.java
 * project:  CCTR
 * @company Hainan Jowe Technology Ltd
 * @author wangxiuwu
 * @create in 2009-5-21
 * @version 1.0
 */
public class SysLogsDaoImpl implements SysLogsDao{
	
	private DBOperation opera;
	private SysLogs sysLogs;
	private static Logger log = Logger.getLogger(SysLogsDaoImpl.class.getName());
	
	public SysLogsDaoImpl(){
	}
	/**
	 * 添加日志
	 * */
	public void addLogs(SysLogs syslog){
		try{
			opera = new DBOperation();
			opera.transcationBegin();
			String sql = "insert into sys_logs (date,messages," +
					"opera_person,opera_orgenization,opera_account)values(SYSDATE(),?,?,?,?)";
			opera.prepStatment(sql);
			opera.setString(1, syslog.getMESSAGES());
			opera.setString(2, syslog.getOpera_person());
			opera.setString(3, syslog.getOpera_orgenization());
			opera.setString(4, syslog.getOpera_account());
			opera.execute();
			opera.transcationCommit();
		} catch (Exception e) {
			log.error(e);
		} finally {
			opera.closePres();
			opera.closeConn();
		}
	}
	/**
	 * 获取列表(有条件，分页取数据)
	 */
	public List<SysLogs> getList(int GUID,String Date, String messages,String opera_person,String opera_orgenization,
			String opera_account,String startDate,String endDate,String ParamName, String ParamValue,String orderBy,int start, int PageSize){
		
		String sqlstr1 = ""; // 条件1
		String sqlstr2 = ""; // 条件2
		String sqlstr3 = ""; // 条件3
		String orderstr="";
		if (messages != null) 
			sqlstr1 = " and MESSAGES='"+messages+"'";

		if (ParamName != null && ParamValue != null) {
			if(ParamName.equals("GUID"))
				sqlstr2 = " and GUID like '%"+ParamValue+"%'";
			if(ParamName.equals("operaPerson"))
				sqlstr2 = " and opera_person like '%"+ParamValue+"%'";
			if (ParamName.equals("MESSAGES"))
				sqlstr2 = " and MESSAGES like '%" + ParamValue + "%'";
			if (ParamName.equals("opera_account"))
				sqlstr2 = " and operaAccount like '%" + ParamValue + "%'";
		}
		if (startDate != null && endDate != null){
			sqlstr3 = " and DATE between DATE_FORMAT('"+startDate+"','%Y-%m-%d') and DATE_FORMAT('"+endDate+"','%Y-%m-%d') ";
		}
		if (orderBy != null&& !"".equals(orderBy)) {
			String str[] = orderBy.split("-");			
			if (str[1].equals("UP"))
				orderstr = " order by " + str[0];
			if (str[1].equals("DOWN"))
				orderstr = " order by " + str[0] + " desc";
		}
		List<SysLogs> logList = new ArrayList<SysLogs>();
		try{
			opera = new DBOperation();
			opera.transcationBegin();
			String sql = "select "
					+ "GUID,DATE,THREAD,LEVEL,CLASSINFO,MESSAGES,EXCEPTION," 
					+"opera_person,opera_orgenization,opera_account "
					+ "from sys_logs "
					+ "where GUID>0 "
					+ sqlstr1+ sqlstr2+ sqlstr3
					+ orderstr
					+ " LIMIT ?,?";
			opera.prepStatment(sql);
			opera.setInt(1, start);
			opera.setInt(2, PageSize);
			ResultSet res = opera.executeQuery();// 执行上面语句
			int i = 0;
			while (res.next()) {
				sysLogs = new SysLogs();
				sysLogs.setGUID(res.getInt(1));
				sysLogs.setDATE(res.getString(2));
				sysLogs.setTHREAD(res.getString(3));
				sysLogs.setLEVEL(res.getString(4));
				sysLogs.setCLASSINFO(res.getString(5));
				sysLogs.setMESSAGES(res.getString(6));
				sysLogs.setEXCEPTION(res.getString(7));
				sysLogs.setOpera_person(res.getString(8));
				sysLogs.setOpera_orgenization(res.getString(9));
				sysLogs.setOpera_account(res.getString(10));
				logList.add(i, sysLogs);
				i++;
			}
		} catch (Exception e) {
			log.error(e);
		} finally {
			opera.closePres();
			opera.closeConn();
		}
		return logList;
	}
	
	public int getListCount(int GUID,String Date, String messages,String opera_person,String opera_orgenization,
			String opera_account,String startDate,String endDate,String ParamName, String ParamValue){
		int totalSize = 0;// 某条件查询下记录的总条数
		String sqlstr1 = ""; // 条件1
		String sqlstr2 = ""; // 条件2
		String sqlstr3 = ""; // 条件3
		if (messages != null) 
			sqlstr1 = " and MESSAGES='"+messages+"'";

		if (ParamName != null && ParamValue != null) {
			if(ParamName.equals("GUID"))
				sqlstr2 = " and GUID like '%"+ParamValue+"%'";
			if(ParamName.equals("opera_person"))
				sqlstr2 = " and opera_person like '%"+ParamValue+"%'";
			if (ParamName.equals("MESSAGES"))
				sqlstr2 = " and MESSAGES like '%" + ParamValue + "%'";
			if (ParamName.equals("opera_account"))
				sqlstr2 = " and opera_account like '%" + ParamValue + "%'";
		}
		if (startDate != null && endDate != null){
			sqlstr3 = " and DATE between DATE_FORMAT('"+startDate+"','%Y-%m-%d') and DATE_FORMAT('"+endDate+"','%Y-%m-%d') ";
		}
		try{
			opera = new DBOperation();
			opera.transcationBegin();			
			String sql = "select count(*) as totalSize "
					+ "from sys_logs "
					+ "where GUID>0 "
					+ sqlstr1+ sqlstr2+sqlstr3;
			opera.prepStatment(sql);
			ResultSet res = opera.executeQuery();
			if (res.next()) {
				totalSize = res.getInt(1);
			}
		} catch (Exception e) {
			log.error(e);
		} finally {
			opera.closePres();
			opera.closeConn();
		}
		return totalSize;
	}
	
	/**
	 * 删除：按ID删除
	 * @param guid
	 * @return boolean
	 */
	public boolean delete(int guid) {
		boolean result = false;
		try{
			opera = new DBOperation();
			opera.transcationBegin();
			String sql = "delete from sys_logs where GUID=?";
			opera.prepStatment(sql);
			opera.setInt(1, guid);
			opera.execute();
			result = opera.transcationCommit();
		} catch (Exception e) {
			log.error(e);
		} finally {
			opera.closePres();
			opera.closeConn();
		}
		return result;
	}
	
	/**
	 * 删除：删除几天前日志
	 * @param days
	 * @return
	 */
	public boolean deleteMore(int days) {
		boolean result = false;
		try{
			opera = new DBOperation();
			opera.transcationBegin();
			String sql = "delete from sys_logs "
					+ "where TO_DAYS(NOW()) - TO_DAYS(date) > ?";			
			opera.prepStatment(sql);
			opera.setInt(1, days);
			opera.execute();
			result = opera.transcationCommit();
		} catch (Exception e) {
			log.error(e);
		} finally {
			opera.closePres();
			opera.closeConn();
		}
		return result;
	}

}
